﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;
using System.IO;

namespace Presentation
{
    public partial class frmManageProduct : Form
    {
        ManageProductBUL mpBUS = new ManageProductBUL();
        string strSearch = "";
        public frmManageProduct()
        {
            InitializeComponent();
            LoadProduct();
        }

        public void LoadProduct()
        {
            dgvProduct.DataSource = mpBUS.LoadProductBUL(strSearch);
            dgvProduct.Columns[0].Visible = false;
            dgvProduct.Columns[6].Visible = false;
            // combobox product name
            cbbProName.DataSource = mpBUS.LoadProductNameBUL().Tables[0];
            cbbProName.ValueMember = "proNameId";
            cbbProName.DisplayMember = "productName";
            // combobox brand
            cbbBrand.DataSource = mpBUS.LoadBrandBUL().Tables[0];
            cbbBrand.DisplayMember = "brandName";
            cbbBrand.ValueMember = "brandId";
            // combobox unit
            cbbUnit.DataSource = mpBUS.LoadUnitBUL().Tables[0];
            cbbUnit.DisplayMember = "unitName";
            cbbUnit.ValueMember = "unitId";

        }

        private void dgvProduct_MouseClick(object sender, MouseEventArgs e)
        {
            string strProName = dgvProduct.SelectedRows[0].Cells[2].Value.ToString();
            cbbProName.SelectedIndex = cbbProName.FindStringExact(strProName);
            string strBrand = dgvProduct.SelectedRows[0].Cells[3].Value.ToString();
            cbbBrand.SelectedIndex = cbbBrand.FindStringExact(strBrand);
            string strUnit = dgvProduct.SelectedRows[0].Cells[4].Value.ToString();
            cbbUnit.SelectedIndex = cbbUnit.FindStringExact(strUnit);
            txtBarcode.Text = dgvProduct.SelectedRows[0].Cells[1].Value.ToString();
            txtPrice.Text = dgvProduct.SelectedRows[0].Cells[5].Value.ToString();
            txtQuantity.Text = dgvProduct.SelectedRows[0].Cells[8].Value.ToString();
            dtpExpiredDate.Value = DateTime.Parse(dgvProduct.SelectedRows[0].Cells[7].Value.ToString()).Date;
            txtBarcode.ReadOnly = true;

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            strSearch = txtSearch.Text;
            LoadProduct();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateProduct();
                LoadProduct();
                dgvProduct.ClearSelection();
                
                Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("Please select product.");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteProduct();
            LoadProduct();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

        public void DeleteProduct()
        {
            if (MessageBox.Show("Are you sure to delete this product?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string strid = dgvProduct.SelectedRows[0].Cells[0].Value.ToString();
                mpBUS.DeleteProduct(Int32.Parse(strid));
            }
        }

        public void UpdateProduct()
        {
            int productId = Int32.Parse(dgvProduct.SelectedRows[0].Cells[0].Value.ToString());
            string barcode = txtBarcode.Text;
            int proNameId = Int32.Parse(cbbProName.SelectedValue.ToString());
            int brandId= Int32.Parse(cbbBrand.SelectedValue.ToString());
            int unitId = Int32.Parse(cbbUnit.SelectedValue.ToString());
            int price = Int32.Parse(txtPrice.Text);
            string expriredDate = dtpExpiredDate.Value.ToString("yyyy/MM/dd");
            //string expriredDate = DateTime.Parse(txtExpiredDate.Text).Date.ToString("yyyy/MM/dd");
            int quantity = Int32.Parse(txtQuantity.Text);

            int i = mpBUS.UpdateProductDAL(productId, barcode, proNameId,
            brandId, unitId, price, expriredDate, quantity);
        }

        // delete
        public void UpdateProduct2()
        {
            Product product = new Product();
            product.ProductId = Int32.Parse(dgvProduct.SelectedRows[0].Cells[0].Value.ToString());
            product.BarCode = txtBarcode.Text;
            product.ProductName.PNameId = Int32.Parse(cbbProName.SelectedValue.ToString());
            product.Brand.BrandId = Int32.Parse(cbbBrand.SelectedValue.ToString());
            product.Unit.UnitId = Int32.Parse(cbbUnit.SelectedValue.ToString());
            product.UnitPrice = Int32.Parse(txtPrice.Text);
            //product.ExpriredDate = DateTime.Parse(txtExpiredDate.Text).Date.ToString("yyyy/MM/dd");
            product.Quantity = Int32.Parse(txtQuantity.Text);

            mpBUS.UpdateProductDAL2(product);

        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            strSearch = "";
            LoadProduct();
        }

        public void Clear()
        {
            txtBarcode.Text = "";
            txtPrice.Text = "";
            txtQuantity.Text = "";
        }

        private void frmManageProduct_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMain main = new frmMain();
            main.Show();
            this.Hide();
        }

    }
}
